<template>
  <div class="purchase-order-page">
    <el-card class="order-card">
      <template #header>
        <div class="card-header">
          <span>采购订单</span>
          <el-button type="primary" @click="handleAdd" v-if="hasPerm('purchase:order:add')">新增订单</el-button>
        </div>
      </template>
      
      <el-table :data="orderList" style="width: 100%">
        <el-table-column prop="orderCode" label="订单编号" width="120" />
        <el-table-column prop="supplierName" label="供应商" width="150" />
        <el-table-column prop="totalAmount" label="总金额" width="120">
          <template #default="scope">
            ¥{{ scope.row.totalAmount.toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column prop="orderDate" label="下单日期" width="120" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button size="small" @click="handleView(scope.row)" v-if="hasPerm('purchase:order:view')">查看</el-button>
            <el-button size="small" type="primary" @click="handleEdit(scope.row)" v-if="hasPerm('purchase:order:edit')">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)" v-if="hasPerm('purchase:order:delete')">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { hasPerm } from '@/utils/permission'
import { ElMessage } from 'element-plus'

const orderList = ref([])

const getStatusType = (status) => {
  const statusMap = {
    'draft': 'info',
    'submitted': 'warning',
    'approved': 'success',
    'rejected': 'danger'
  }
  return statusMap[status] || 'info'
}

const getStatusText = (status) => {
  const statusMap = {
    'draft': '草稿',
    'submitted': '已提交',
    'approved': '已审核',
    'rejected': '已驳回'
  }
  return statusMap[status] || '未知'
}

const handleAdd = () => {
  ElMessage.info('新增采购订单功能')
}

const handleView = (row) => {
  ElMessage.info(`查看订单：${row.orderCode}`)
}

const handleEdit = (row) => {
  ElMessage.info(`编辑订单：${row.orderCode}`)
}

const handleDelete = (row) => {
  ElMessage.info(`删除订单：${row.orderCode}`)
}

onMounted(() => {
  // 模拟数据
  orderList.value = [
    {
      orderCode: 'PO001',
      supplierName: '供应商A',
      totalAmount: 10000.00,
      orderDate: '2025-07-30',
      status: 'draft'
    },
    {
      orderCode: 'PO002',
      supplierName: '供应商B',
      totalAmount: 15000.00,
      orderDate: '2025-07-29',
      status: 'submitted'
    },
    {
      orderCode: 'PO003',
      supplierName: '供应商C',
      totalAmount: 8000.00,
      orderDate: '2025-07-28',
      status: 'approved'
    }
  ]
})
</script>

<style scoped>
.purchase-order-page {
  padding: 20px;
}

.order-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style> 